package smartcampus.scjava.controller.sixth;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import smartcampus.scjava.POJO.DO.CsmsUser;
import smartcampus.scjava.POJO.DTO.CsmsUserDTO;
import smartcampus.scjava.custom.ResultVO;
import smartcampus.scjava.service.CsmsUserService;

import javax.annotation.Resource;

/**
 * @author 雨辰YC
 * @date 2023/12/7 0:32
 * @description CsmsUserController
 */
@RestController
@RequestMapping("/csms")
public class CsmsUserController {

    @Autowired
    CsmsUserService csmsUserService;

    /**
     * 路径添加/show：让拦截器放行
     * 参数传递方式：{}，这类参数传递方式，则需要将参数添加到访问路径之中
     * @param username
     * @return
     */
    @GetMapping("/generateUser")
    @ApiOperation("生成用户")
    public ResultVO<String> generateUser(@RequestParam(value = "username") String username){
        boolean isGenerateUser = csmsUserService.generateUser(username);
        System.out.println("添加成功与否: "+ isGenerateUser);
        if (isGenerateUser && !username.equals("")){
            return ResultVO.success("添加成功");
        }else {
            return ResultVO.fail(403, "添加失败");
        }
    }

    @GetMapping("/loadUser")
    @ApiOperation("加载用户")
    public ResultVO<?> loadUser(@RequestParam(value = "username") String username){
        CsmsUser csmsUser = csmsUserService.getCsmsUserByUserName(username);
        CsmsUserDTO csmsUserDTO = new CsmsUserDTO();
        if (csmsUser == null){
            return ResultVO.fail(204, "用户名不存在");
        }
        csmsUserDTO.setId(csmsUser.getId());
        csmsUserDTO.setUserID(csmsUser.getUserID());
        csmsUserDTO.setUsername(csmsUser.getUsername());
        csmsUserDTO.setRole(csmsUser.getRole());
        return ResultVO.success(csmsUserDTO);
    }

}
